var arr_shuoming=["炸药。你买到它以后，当你抓到较重而且钱不多的物品时，按下上方向键即可将它炸毁，以便节省时间。",
                  "生力水。买了它以后，在下一关中你的力气将会增加，即抓东西的速度会增加。其功效为一关。",
                  "幸运草。它将增强你在下一关中从色袋中取得好东西的机遇。其效用为一关。",
                  "石头收藏书。石头将有三次机遇会变成相当有价值的物品。其功效为一关。",
                  "优质钻石。在下一关钻石将变得相当值钱。其效用为一关。"
                  ];
var ShopPage = cc.Layer.extend({
	sprite:null,
	btn_Start:null,
	btn_Sure:null,
	btn_DaoJu1:null,
	btn_DaoJu2:null,
	btn_DaoJu3:null,
	btn_DaoJu4:null,
	btn_DaoJu5:null,
	num_JiaGe1:0,
	num_JiaGe2:0,
	num_JiaGe3:0,
	num_JiaGe4:0,
	num_JiaGe5:0,
	num_Money:0,
	txt_ShuoMing:null,
	txt_Money:null,
	pic_Shopback:null,
	pic_Shoptop:null,
	text_Shop:null,
	sp_Target:null,
	ctor:function () {
		//////////////////////////////
		// 1. super init first
		this._super();

		/////////////////////////////
		// 2. add a menu item with "X" image, which is clicked to quit the program
		//    you may modify it.
		// ask the window size
		var size = cc.winSize;
		// add a "close" icon to exit the progress. it's an autorelease object
		var shopScene=ccs.load(res.shopscene_json).node;
		this.addChild(shopScene);
		sp_Target=null;
		g_Array_DaoJu=[0,0,0,0,0];
		pic_Shopback=shopScene.getChildByName("Image_1");
		pic_Shopback.setScaleY(theScaleH);
		
		text_Shop=shopScene.getChildByName("Text_1_0");
		pic_Shopback=shopScene.getChildByName("Image_3");
		txt_ShuoMing=shopScene.getChildByName("Text_1");
		txt_ShuoMing.setString("恭喜你，过关了！！！下一关的目标分数是："+arr_mubiaofenshu[gameLevel]+"分 哦。购买自己喜欢的道具，准备冲刺下一关吧。。。。");
		btn_Start=ccui.helper.seekWidgetByName(shopScene, "Button_2");
		btn_Sure=ccui.helper.seekWidgetByName(shopScene, "Button_1");
		btn_DaoJu1=ccui.helper.seekWidgetByName(shopScene, "Button_3");
		btn_DaoJu2=ccui.helper.seekWidgetByName(shopScene, "Button_4");
		btn_DaoJu3=ccui.helper.seekWidgetByName(shopScene, "Button_5");
		btn_DaoJu4=ccui.helper.seekWidgetByName(shopScene, "Button_6");
		btn_DaoJu5=ccui.helper.seekWidgetByName(shopScene, "Button_7");
		txt_Money=shopScene.getChildByName("top1_txt");
		
		btn_Sure.setVisible(false);
		if(Math.random()>0.5) btn_DaoJu1.setVisible(false);
		if(Math.random()>0.5) btn_DaoJu2.setVisible(false);
		if(Math.random()>0.5) btn_DaoJu3.setVisible(false);
		if(Math.random()>0.5) btn_DaoJu4.setVisible(false);
		if(Math.random()>0.5) btn_DaoJu5.setVisible(false);
		text_Shop.y+=(size.height-1024)*0.8;
		pic_Shopback.y+=(size.height-1024)*0.8;
		txt_Money.y+=(size.height-1024)*0.8;
		btn_Start.y+=(size.height-1024)*0.8;
		txt_ShuoMing.y+=(size.height-1024)*0.5;
		
		
		txt_Money.setString(g_Value_gameScore);
		btn_Start.addTouchEventListener(this.backClick);
		btn_Sure.addTouchEventListener(this.buyClick);
		btn_DaoJu1.addTouchEventListener(this.daojuClick);
		btn_DaoJu2.addTouchEventListener(this.daojuClick);
		btn_DaoJu3.addTouchEventListener(this.daojuClick);
		btn_DaoJu4.addTouchEventListener(this.daojuClick);
		btn_DaoJu5.addTouchEventListener(this.daojuClick);
		
		
		//随机各道具价格
		num_Money=0;
		num_JiaGe1=100+Math.floor(Math.random()*200);
		num_JiaGe2=200+Math.floor(Math.random()*300);
		num_JiaGe3=Math.floor(Math.random()*100);
		num_JiaGe4=Math.floor(Math.random()*100);
		num_JiaGe5=200+Math.floor(Math.random()*300);
		
		var txt1 = new cc.LabelTTF("$ "+num_JiaGe1, "Arial", 24);
		txt1.setColor(cc.color(0, 0, 0));
		txt1.x = 10;
		txt1.y = -20;
		btn_DaoJu1.addChild(txt1);
		
		var txt2 = new cc.LabelTTF("$ "+num_JiaGe2, "Arial", 24);
		txt2.setColor(cc.color(0, 0, 0));
		txt2.x = 35;
		txt2.y = -20;
		btn_DaoJu2.addChild(txt2);
		
		var txt3 = new cc.LabelTTF("$ "+num_JiaGe3, "Arial", 24);
		txt3.setColor(cc.color(0, 0, 0));
		txt3.x = 20;
		txt3.y = -20;
		btn_DaoJu3.addChild(txt3);
		
		var txt4 = new cc.LabelTTF("$ "+num_JiaGe4, "Arial", 24);
		txt4.setColor(cc.color(0, 0, 0));
		txt4.x = 40;
		txt4.y = -20;
		btn_DaoJu4.addChild(txt4);
		
		var txt5 = new cc.LabelTTF("$ "+num_JiaGe5, "Arial", 24);
		txt5.setColor(cc.color(0, 0, 0));
		txt5.x = 30;
		txt5.y = -20;
		btn_DaoJu5.addChild(txt5);
		
		return true;
	},
	daojuClick:function(sender,type)
	{
		switch (type) {        
		//pageView当前所在的page的index发生了变化。
		case ccui.Widget.TOUCH_ENDED:
//			helloworldl.resetItems(0);
			txt_ShuoMing.setVisible(true);
			btn_Sure.setVisible(true);
			sp_Target=sender;
			cc.log("jjjjj=",sender.getName().charAt(7));
			if(sender.getName() == "Button_3"){
				num_Money=num_JiaGe1;
				txt_ShuoMing.setString(arr_shuoming[0]);
			}
			if(sender.getName() == "Button_4"){
				num_Money=num_JiaGe2;
				txt_ShuoMing.setString(arr_shuoming[1]);
			}
			if(sender.getName() == "Button_5"){
				num_Money=num_JiaGe3;
				txt_ShuoMing.setString(arr_shuoming[2]);
			}
			if(sender.getName() == "Button_6"){
				num_Money=num_JiaGe4;
				txt_ShuoMing.setString(arr_shuoming[3]);
			}
			if(sender.getName() == "Button_7"){
				num_Money=num_JiaGe5;
				txt_ShuoMing.setString(arr_shuoming[4]);
			}
			break;        
		default:  break;
		}
	},
	buyClick:function(sender,type)
	{
		switch (type) {        
		//pageView当前所在的page的index发生了变化。
		case ccui.Widget.TOUCH_ENDED:
			if(sp_Target!=null){
				if(g_Value_gameScore>=num_Money){
					txt_ShuoMing.setVisible(false);
					sp_Target.setVisible(false);
					g_Value_gameScore-=num_Money;
					txt_Money.setString(g_Value_gameScore);
					num_Money=0;
					var num=Math.floor(sp_Target.getName().charAt(7))-3;
					g_Array_DaoJu[num]=1;
					if(num==3) g_Array_DaoJu[num]=3;
					cc.log("arr=",g_Array_DaoJu);
					sp_Target=null;
					btn_Sure.setVisible(false);
				}else {
					txt_ShuoMing.setString("不够钱,加油哦!!!");
				}
			}
//			helloworldl.resetItems(0);
			//cc.director.runScene(new GameCenterScene());
			break;        
		default:  break;
		}
	},
	backClick:function(sender,type)
	{
		switch (type) {        
		//pageView当前所在的page的index发生了变化。
		case ccui.Widget.TOUCH_ENDED:
//			helloworldl.resetItems(0);
			cc.director.runScene(new GameCenterScene());
			break;        
		default:  break;
		}
	}
});

var ShopPageScene = cc.Scene.extend({
	onEnter:function () {
		this._super();
		var layer = new ShopPage();
		this.addChild(layer);
	}
});

